<template>
  <div>个人信息</div>
  <el-card>
    <el-row>
      <el-form
        :model="userInfoForm"
        :rules="rules"
        ref="userInfoFormRef"
        label-width="auto"
        style="max-width: 600px"
      >
        <el-form-item label="姓名" prop="teacherName">
          <el-input v-model="userInfoForm.teacherName">
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="emil">
          <el-input v-model="userInfoForm.emil">
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model.number="userInfoForm.phone">
            <template #prefix>
              <el-icon><Iphone /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="身份" prop="type">
          <el-select
            v-model="userInfoForm.type"
            placeholder="请选择身份"
            style="width: 240px"
          >
            <el-option label="学校老师" :value="1" />
            <el-option label="公司人员" :value="0" />
          </el-select>
          <template #prefix>
            <el-icon><UserFilled /></el-icon>
          </template>
        </el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form>
    </el-row>
  </el-card>
</template>

<script setup>
import { getuserInfo, putuserInfo } from "@/api/userInfo";
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { UserFilled, Iphone, Message, User } from "@element-plus/icons-vue";

const userInfoForm = reactive({
  teacherName: "",
  phone: null,
  emil: "",
  updateTime: "",
  type: null, // 初始化时不设置默认值
});

const userInfoFormRef = ref(null);

// 表单验证规则
const rules = {
  teacherName: [
    { required: true, message: "请输入姓名", trigger: "blur" },
    { type: "string", message: "姓名必须为字符串", trigger: "blur" },
  ],
  emil: [
    { required: true, message: "请输入邮箱地址", trigger: "blur" },
    {
      type: "email",
      message: "请输入正确的邮箱地址格式",
      trigger: "blur,change",
    },
  ],
  type: [{ required: true, message: "请选择身份", trigger: "change" }],
  phone: [
    { required: true, message: "请输入手机号码", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "手机号码格式不正确",
      trigger: "blur",
    },
    {
      validator: (rule, value, callback) => {
        if (typeof value !== "number" || isNaN(value)) {
          callback(new Error("手机号码必须为数字"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
};

// 提交函数
const onSubmit = async () => {
  try {
    await userInfoFormRef.value.validate();
    const response = await putuserInfo(userInfoForm);
    if (response.data.code === 200) {
      ElMessage.success("信息更新成功");
    } else {
      ElMessage.error("信息更新失败：" + response.message);
    }
  } catch (error) {
    console.error("提交失败:", error);
    ElMessage.error("提交失败");
  }
};

onMounted(() => {
  fetchUserInfo();
});

// 获取数据
const fetchUserInfo = async () => {
  try {
    const res = await getuserInfo();
    userInfoForm.teacherName = res.data.data.teacherName || "";
    userInfoForm.phone = res.data.data.phone
      ? Number(res.data.data.phone)
      : null;
    userInfoForm.type = res.data.data.type; // 直接赋值 type
    userInfoForm.emil = res.data.data.emil || "";
  } catch (error) {
    console.error("获取用户信息失败:", error);
    ElMessage.error("获取用户信息失败");
  }
};
</script>

<style scoped></style>
